package com.ruoyi.project.cyq.order.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.cyq.order.domain.*;
import com.ruoyi.project.cyq.order.mapper.OrderClientMapper;
import com.ruoyi.project.cyq.order.mapper.OrderItemProjectMapper;
import com.ruoyi.project.cyq.order.mapper.OrderProductMapper;
import com.ruoyi.project.cyq.order.mapper.OrderScheduleMapper;
import com.ruoyi.project.cyq.order.service.IOrderGenreService;
import com.ruoyi.project.cyq.order.service.IOrderService;
import org.apache.poi.ss.formula.functions.Odd;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 *
 */
@Controller
@RequestMapping("/cyq/order")
public class OrderController extends BaseController {
    //声明资源地址的公共前缀
    private String prefix="cyq/order";

    @Autowired
    private IOrderService orderService;
    
    @Autowired
    private IOrderGenreService orderGenreService;
    
    @Autowired
    private OrderScheduleMapper orderScheduleMapper;
    @Autowired
    private OrderProductMapper orderProductMapper;
    @Autowired
    private OrderClientMapper orderClientMapper;
    @Autowired
    private OrderItemProjectMapper orderItemProjectMapper;

    //声明单元方法:跳转订单类别页面
    @RequestMapping("/genre")//接收get请求,实际省略了一个斜杠
    public String orderGenre(){
        return prefix+"/orderGenre";
    }

    //查询所有订单类别
    @RequestMapping("/genre/list")
    @ResponseBody
    public TableDataInfo selectList(){
        startPage();
        List<OrderGenre> orderGenres = orderGenreService.selectList();
        return getDataTable(orderGenres);
    }

    /**
     * 新增订单类别窗口
     */
    @GetMapping("/genre/add")
    public String add()
    {
        return prefix + "/genreAdd";
    }

    //添加订单类别
    @RequiresPermissions("cyp:order:genre:add")
    @PostMapping("/genre/add")
    @ResponseBody
    public AjaxResult genreAdd(@Validated OrderGenre orderGenre)
    {
        if(UserConstants.CYQ_ORDER_GENRE_NOT_UNIQUE.equals(orderGenreService.checkGenreNameUnique(orderGenre))){
            return error("新增类型'" + orderGenre.getGenreName() + "'失败，订单类别名称已存在");
        }
        return toAjax(orderGenreService.insertGenre(orderGenre));
    }

    /**
     * 修改订单类别窗口
     */
    @GetMapping("/genre/edit/{id}")
    @ResponseBody
    public String edit(@PathVariable("id") Long id, ModelMap modelMap)
    {
        OrderGenre orderGenre = orderGenreService.selectByGenreId(id);
        modelMap.put("orderGenre", orderGenre);
        return prefix + "/genreEdit";
    }

    //修改订单类别
    @RequiresPermissions("cyp:order:genre:edit")
    @RequestMapping("/genre/edit")
    @ResponseBody
    public AjaxResult genreEdit(@Validated OrderGenre orderGenre){
        if(UserConstants.CYQ_ORDER_GENRE_NOT_UNIQUE.equals(orderGenreService.checkGenreNameUnique(orderGenre))){
            return error("新增类型'" + orderGenre.getGenreName() + "'失败，订单类别名称已存在");
        }
        return toAjax(orderGenreService.UpdateById(orderGenre));
    }

    //类型名称唯一性检查
    @PostMapping("/genre/checkGenreNameUnique")
    @ResponseBody
    public String checkGenreNameUnique(OrderGenre orderGenre){
        String s = orderGenreService.checkGenreNameUnique(orderGenre);
        return s;
    }

    //声明单元方法:跳转订单管理页面
    @RequiresPermissions("cyp:order:view")
    @GetMapping()//接收get请求,实际省略了一个斜杠
    public String order(){
        return prefix+"/order";
    }

    //页面内部跳转
    @RequestMapping("/detail/{id}")
    public String select(@PathVariable("id") Long id, ModelMap modelMap){
//        先通过id查询出订单
        Order order=orderService.selectOrderById(id);
        OrderClient orderClient = orderClientMapper.selectByClientId(order.getClientId());
        OrderItemProject orderItemProject = orderItemProjectMapper.selectByNumber(order.getOrderNumber());
        //时间格式化
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String startTime = sdf.format(orderItemProject.getProjectStartTime());
        String endTime = sdf.format(orderItemProject.getProjectEndTime());
        modelMap.addAttribute("endTime",endTime);
        modelMap.put("startTime",startTime);
        OrderProduct orderProduct = orderProductMapper.selectByProductId(order.getProductId());
        OrderSchedule orderSchedule = orderScheduleMapper.selectByOrderNumber(order.getOrderNumber());
        OrderGenre orderGenre = orderGenreService.selectByGenreId(order.getGenreId());
        System.out.println("_____________________________");
        System.out.println(order);
        System.out.println("_____________________________");
        System.out.println(orderClient);
        System.out.println("_____________________________");
        System.out.println(orderItemProject);
        System.out.println("_____________________________");
        System.out.println(orderProduct);
        System.out.println("_____________________________");
        System.out.println(orderSchedule);
        System.out.println("_____________________________");
        modelMap.put("oClient",orderClient);
        modelMap.put("oProject",orderItemProject);
        modelMap.put("oProduct",orderProduct);
        modelMap.put("oSchedule",orderSchedule);
        modelMap.put("order", order);
        modelMap.put("oGenre",orderGenre);
        return prefix+"/OrderDetail";
    }

    @RequiresPermissions("cyq:order:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo selectList(Order order) {
        /**
         * 1.该方法底层调用PageHelper分页插件，自定完成分页查询的相关设置以及分页Sql语句的拼接
         * 也就是会自动的在我们声明的查询公告类型的Sql语句后拼接limit关键字
         * 2.startPage方法的底层会自动的调用HttpServletRequest对象获取此次分页请求中的
         *  分页条件，pageNum和pageSize。
         *
         */
        startPage();
        /**
         * 调用业务层完成分页查询，并获取分页结果
         */
        List<Order> list=orderService.selectOrderList(order);
        /**
         * getDataTable方法将存储了分页结果的集合转换为TableDataInfo对象并返回
         * 然后响应给前台。
         * 注意：
         *  前台页面中接收的是TableDataInfo对象的json数据，不是list集合，相当于    EasyUI的DataGrid
         *  的方式，DataGrid接收的是PageResult，只不过以前PageResult是我们自己声明，现在在若依
         *  项目中已经提前声明好了。
         *
         */
        return getDataTable(list);
    }

}
